#include <QTimer>
#include <QMessageBox>
#include "SrvConnection.h"
#include "DlgServerConTest.h"
#include "scImpl.h"

#define FCT "DlgServerConTest::DlgServerConTest"
DlgServerConTest::DlgServerConTest(SrvConnection & server, bool autoClose, QWidget *parent) :
	QDialog(parent), state(CT_SETUP), ac(autoClose), timer(0), srv(server), transId(-1)
{
	ctrc(Trc::WORK,APP,FCT)<<"start"<<endl;
	ui.setupUi(this);
	ui.actionText->setText(tr("setting up connector"));
	updateStatus();
	ui.progressBar->setMinimum(0);
	ui.progressBar->setMaximum(100);
	ui.progressBar->setValue(0);
	//ui.progressBar->setEnabled(false);
	// very strange: if you cancel this dialog while the timer is
	// running, the app crashes when deleting srv...
	ui.cancelButton->setEnabled(false);
	timer=new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(timerTic()));
	timer->start(20);
	connect(&srv, SIGNAL(testReadySig(bool, unsigned long)), this, SLOT(testReadySl(bool, unsigned long)));
	connect(&srv, SIGNAL(infoActionText(QString)), this, SLOT(updateActionSl(QString)));
	connect(&srv, SIGNAL(infoStatusText(QString)), this, SLOT(updateHttpStatusSl(QString)));
	connect(&srv, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateProgressSl(int, int)));
	connect(&srv, SIGNAL(dataSendProgress(int, int)), this, SLOT(updateProgressSl(int, int)));
	connect(ui.detailsButton, SIGNAL(clicked()), this, SLOT(detailsSl()));
}

#undef FCT
#define FCT "DlgServerConTest::~DlgServerConTest"
DlgServerConTest::~DlgServerConTest()
{
	timer->stop();
	delete timer;
}

#undef FCT
#define FCT "DlgServerConTest::updateStatus"
void DlgServerConTest::updateStatus()
{
	QString text;
	switch(state)
	{
	case CT_SETUP:
	case CT_HEAD:
		text=tr("testing");
		break;
	case CT_FAILED:
		text=tr("FAILED");
		break;
	case CT_SUCCESS:
		text=tr("successful");
		break;
	}
	if(!httpStatusText.isEmpty())
	{
		text+=" (http::";
		text+=httpStatusText;
		text+=")";
	}
	ui.statusText->setText(text);
}

#undef FCT
#define FCT "DlgServerConTest::timerTic"
void DlgServerConTest::timerTic()
{
	ctrc(Trc::INNERLOOP,APP,FCT)<<"state "<<state<<endl;
	switch(state)
	{
	case CT_SETUP:
	{
		int val(ui.progressBar->value());
		val+=5;
		if(val>100)
		{
			state=CT_HEAD;
			ui.cancelButton->setEnabled(true);
		}
		else
			ui.progressBar->setValue(val);
		break;
	}
	case CT_HEAD:
	{
		timer->stop();
		ui.actionText->setText(tr("sending request"));
		updateStatus();
		ui.progressBar->setMaximum(0);
		ui.progressBar->setValue(0);
		srv.startTest(); // output through signals
		break;
	}
	case CT_FAILED:
	case CT_SUCCESS:
		break;
	}
}

#undef FCT
#define FCT "DlgServerConTest::testReadySl"
void DlgServerConTest::testReadySl(bool success, unsigned long transId)
{
	ctrc(Trc::WORK,APP,FCT)<<"state "<<state<<" success "<<success<<endl;
	details=srv.getData(transId)->data();
	srv.freeData(transId);
	if(success)
	{
		state=CT_SUCCESS;
		ui.cancelButton->setEnabled(false);
		ui.okButton->setEnabled(true);
		if(ac)
			ui.okButton->click();
	}
	else
	{
		state=CT_FAILED;
		QMessageBox::warning(this, tr("Connection Problem"), srv.errorString());
		ui.cancelButton->setEnabled(true);
		ui.okButton->setEnabled(false);
	}
	ui.progressBar->setMaximum(100);
	ui.progressBar->setValue(100);
	ui.detailsButton->setEnabled(true);
	updateStatus();
}

#undef FCT
#define FCT "DlgServerConTest::updateActionSl"
void DlgServerConTest::updateActionSl(QString action)
{
	ui.actionText->setText(action);
}

#undef FCT
#define FCT "DlgServerConTest::updateHttpStatusSl"
void DlgServerConTest::updateHttpStatusSl(QString httpStatus)
{
	httpStatusText=httpStatus;
	updateStatus();
}

#undef FCT
#define FCT "DlgServerConTest::updateProgressSl"
void DlgServerConTest::updateProgressSl(int current, int total)
{
	ui.progressBar->setMaximum(total);
	ui.progressBar->setValue(current);
}

#undef FCT
#define FCT "DlgServerConTest::detailsSl"
void DlgServerConTest::detailsSl()
{
	QMessageBox::information(this, tr("http Header Details"), details);
}
